package com.healthdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.healthdata.dao.HealthPersonnelMapper;
import com.healthdata.entity.HealthPersonnel;
import com.healthdata.service.HealthPersonnelService;
import com.healthdata.vo.HealthPersonnelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HealthPersonnelServiceImpl implements HealthPersonnelService {

    @Autowired
    private HealthPersonnelMapper healthPersonnelMapper;

    @Override
    public List<HealthPersonnelVO> listAll() {
        List<HealthPersonnel> entityList = healthPersonnelMapper.selectList(null);
        return convertToVOList(entityList);
    }

    @Override
    public List<HealthPersonnelVO> listByRegion(String region) {
        QueryWrapper<HealthPersonnel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("region", region);
        return convertToVOList(healthPersonnelMapper.selectList(queryWrapper));
    }

    @Override
    public HealthPersonnelVO countTotalPersonnel() {
        // 查询所有记录并计算总和
        List<HealthPersonnel> entities = healthPersonnelMapper.selectList(null);

        HealthPersonnelVO totalVO = new HealthPersonnelVO();
        totalVO.setRegion("总计");

        // 计算各项人员总和
        HealthPersonnel sumEntity = new HealthPersonnel();
        sumEntity.setTotal(entities.stream().mapToInt(e -> e.getTotal() != null ? e.getTotal() : 0).sum());
        sumEntity.setHealthTechnicians(entities.stream().mapToInt(e -> e.getHealthTechnicians() != null ? e.getHealthTechnicians() : 0).sum());
        sumEntity.setRuralDoctors(entities.stream().mapToInt(e -> e.getRuralDoctors() != null ? e.getRuralDoctors() : 0).sum());
        sumEntity.setAdministrators(entities.stream().mapToInt(e -> e.getAdministrators() != null ? e.getAdministrators() : 0).sum());
        sumEntity.setSupportStaff(entities.stream().mapToInt(e -> e.getSupportStaff() != null ? e.getSupportStaff() : 0).sum());

        // 转换为VO并计算占比
        BeanUtils.copyProperties(sumEntity, totalVO);
        calculateRatios(totalVO);

        return totalVO;
    }

    // 实体转VO列表
    private List<HealthPersonnelVO> convertToVOList(List<HealthPersonnel> entities) {
        List<HealthPersonnelVO> voList = new ArrayList<>();
        for (HealthPersonnel entity : entities) {
            voList.add(convertToVO(entity));
        }
        return voList;
    }

    // 单个实体转VO
    private HealthPersonnelVO convertToVO(HealthPersonnel entity) {
        HealthPersonnelVO vo = new HealthPersonnelVO();
        BeanUtils.copyProperties(entity, vo);
        calculateRatios(vo); // 计算占比
        return vo;
    }

    // 计算占比和人员类型（核心逻辑）
    private void calculateRatios(HealthPersonnelVO vo) {
        if (vo.getTotal() != null && vo.getTotal() > 0) {
            // 计算占比（保留原始小数）
            double techRatio = (double) vo.getHealthTechnicians() / vo.getTotal();
            double ruralRatio = (double) vo.getRuralDoctors() / vo.getTotal();

            vo.setTechRatio(techRatio);
            vo.setRuralDoctorRatio(ruralRatio);

            // 根据卫生技术人员占比分类
            if (techRatio > 0.8) {
                vo.setPersonnelType("技术密集型");
            } else if (techRatio > 0.6) {
                vo.setPersonnelType("均衡型");
            } else {
                vo.setPersonnelType("辅助型");
            }
        }
    }


}